package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import com.greensock.*;

    public class BookmarksBrowse extends Sprite
    {
        var btnsArray:Array;
        var fadeSpeed:Number;
        var bookmarksImgShell:BookmarksImgShell;
        var msgText:ShortText;
        public var coverSprite:Sprite;
        var coverSpriteTop:Sprite;
        var bookmarksNote:BookmarksNote;
        var currNavId:int = 0;
        var bookmarksPagesShell:BookmarksPagesShell;
        var bookmarksXMLLoader:URLLoader;
        var myRoot:MovieClip;
        var myBookmarksID:String;
        var longText:LongText;
        var myLink:BookmarkSaveLink;
        var coverSpriteBot:Sprite;
        var shellsArray:Array;
        var bookmarksXML:XML;
        var navArray:Array;

        public function BookmarksBrowse(param1:MovieClip, param2:Number) : void
        {
            navArray = new Array("My Pages", "My Images", "My Notes");
            currNavId = 0;
            visible = false;
            alpha = 0;
            myRoot = param1;
            fadeSpeed = param2;
            loadBookmarks();
            buildLongTitle();
            msgText = new ShortText(myRoot, "", 0);
            msgText.field.width = 200;
            msgText.alpha = 0;
            msgText.visible = true;
            addChild(msgText);
            buildCoverSprite();
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            return;
        }

        function buildCoverSprite() : void
        {
            myLink = new BookmarkSaveLink();
            myLink.field1.field.text = myRoot.path + "www/#/bookmarks/browse/" + myRoot.myBookmarksID + "/";
            myLink.field1.field.addEventListener(FocusEvent.FOCUS_IN, focusInListener, false, 0, true);
            myLink.saveBtn.buttonMode = true;
            myLink.saveBtn.useHandCursor = true;
            myLink.saveBtn.addEventListener(MouseEvent.CLICK, saveBtnClick, false, 0, true);
            myLink.saveBtn.addEventListener(MouseEvent.ROLL_OVER, saveBtnRollOver, false, 0, true);
            myLink.saveBtn.addEventListener(MouseEvent.ROLL_OUT, saveBtnRollOut, false, 0, true);
            addChild(myLink);
            coverSprite = new Sprite();
            coverSprite.buttonMode = true;
            coverSprite.useHandCursor = false;
            coverSprite.alpha = 0;
            coverSprite.visible = false;
            addChild(coverSprite);
            coverSpriteTop = new Sprite();
            coverSpriteTop.graphics.beginFill(16777215, 1);
            coverSpriteTop.graphics.drawRect(0, 0, 1, 1);
            coverSpriteTop.graphics.endFill();
            coverSprite.addChild(coverSpriteTop);
            coverSpriteBot = new Sprite();
            coverSpriteBot.graphics.beginFill(16777215, 1);
            coverSpriteBot.graphics.drawRect(0, 0, 1, 1);
            coverSpriteBot.graphics.endFill();
            coverSprite.addChild(coverSpriteBot);
            return;
        }

        function initBookmarksXML(event:Event) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:* = undefined;
            var _loc_5:* = undefined;
            bookmarksXML = XML(bookmarksXMLLoader.data);
            myRoot.bookmarksXML = bookmarksXML;
            myRoot.localImagesXML = <images></images>;
            _loc_2 = 0;
            while (_loc_2 < myRoot.bookmarksXML.images.item.length())
            {
                
                _loc_4 = myRoot.bookmarksXML.images.item[_loc_2];
                myRoot.localImagesXML.images = myRoot.localImagesXML.images + <image/>;
                myRoot.localImagesXML.image[_loc_2].@id = _loc_4.@id;
                myRoot.localImagesXML.image[_loc_2].@parentId = _loc_4.@parentId;
                _loc_2++;
            }
            myRoot.localPagesXML = <pages></pages>;
            _loc_3 = 0;
            while (_loc_3 < myRoot.bookmarksXML.bookmarks.item.length())
            {
                
                trace("j " + _loc_3);
                _loc_5 = myRoot.bookmarksXML.bookmarks.item[_loc_3];
                myRoot.localPagesXML.pages = myRoot.localPagesXML.pages + <page/>;
                myRoot.localPagesXML.page[_loc_3].@id = _loc_5.@id;
                _loc_3++;
            }
            navArray[0] = "My Pages " + myRoot.localPagesXML.page.length();
            navArray[1] = "My Images " + myRoot.localImagesXML.image.length();
            buildNav();
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            return;
        }

        public function fadeOutShell() : void
        {
            visible = false;
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            if (coverSprite != null)
            {
                coverSprite.x = 0;
                coverSprite.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH));
                coverSpriteTop.x = 8 * (myRoot.unitW + myRoot.gutterW);
                coverSpriteTop.width = 4 * (myRoot.unitW + myRoot.gutterW);
                coverSpriteTop.height = myRoot.unitH / 2;
                coverSpriteBot.y = myRoot.unitH + myRoot.gutterH;
                coverSpriteBot.width = 12 * (myRoot.unitW + myRoot.gutterW);
                coverSpriteBot.height = 4 * (myRoot.unitH + myRoot.gutterH);
            }
            if (longText != null)
            {
                longText.x = Math.round(4 * (myRoot.unitW + myRoot.gutterW));
                longText.y = myRoot.headerH + (myRoot.unitH + myRoot.gutterH) - 4 * myRoot.unitW / 75;
                var _loc_2:* = myRoot.unitW / 75;
                longText.field.scaleY = myRoot.unitW / 75;
                longText.field.scaleX = _loc_2;
            }
            if (msgText != null)
            {
                msgText.x = Math.round(4 * (myRoot.unitW + myRoot.gutterW));
                msgText.y = myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH);
                var _loc_2:* = myRoot.unitW / 75;
                msgText.field.scaleY = myRoot.unitW / 75;
                msgText.field.scaleX = _loc_2;
            }
            if (myLink != null)
            {
                var _loc_2:* = myRoot.unitW / 75;
                myLink.field1.field.scaleY = myRoot.unitW / 75;
                myLink.field1.field.scaleX = _loc_2;
                myLink.field1.inputBG.width = Math.round(6 * myRoot.unitW + 5 * myRoot.gutterW);
                myLink.field1.inputBG.height = Math.round(26 * myRoot.unitW / 75);
                var _loc_2:* = Math.round(6 * (myRoot.unitW + myRoot.gutterW));
                myLink.saveBtn.x = Math.round(6 * (myRoot.unitW + myRoot.gutterW));
                myLink.saveBG.x = _loc_2;
                var _loc_2:* = Math.round(myLink.field1.y);
                myLink.saveBtn.y = Math.round(myLink.field1.y);
                myLink.saveBG.y = _loc_2;
                var _loc_2:* = Math.round(80 * myRoot.unitW / 75);
                myLink.saveBtn.width = Math.round(80 * myRoot.unitW / 75);
                myLink.saveBG.width = _loc_2;
                var _loc_2:* = Math.round(26 * myRoot.unitW / 75);
                myLink.saveBtn.height = Math.round(26 * myRoot.unitW / 75);
                myLink.saveBG.height = _loc_2;
                myLink.mySave.x = myLink.saveBG.x + 3;
                myLink.mySave.y = myLink.saveBG.y + 1;
                var _loc_2:* = myRoot.unitW / 75;
                myLink.mySave.scaleY = myRoot.unitW / 75;
                myLink.mySave.scaleX = _loc_2;
                myLink.saveArrow.x = Math.round(myLink.saveBG.x + myLink.saveBG.width - 10 * myRoot.unitW / 75);
                myLink.saveArrow.y = Math.round(myLink.saveBG.y + 10 * myRoot.unitW / 75);
                myLink.x = Math.round(4 * (myRoot.unitW + myRoot.gutterW));
                myLink.y = Math.round(myRoot.headerH + 2 * myRoot.unitH + myRoot.gutterH - 8 * myRoot.unitW / 75 - myLink.height);
            }
            if (btnsArray != null)
            {
                _loc_1 = 0;
                while (_loc_1 < btnsArray.length)
                {
                    
                    var _loc_2:* = myRoot.unitW / 75;
                    btnsArray[_loc_1].field.scaleY = myRoot.unitW / 75;
                    btnsArray[_loc_1].field.scaleX = _loc_2;
                    btnsArray[_loc_1].bg.width = myRoot.unitW;
                    btnsArray[_loc_1].bg.height = btnsArray[_loc_1].field.height;
                    btnsArray[_loc_1].x = Math.round(8 * (myRoot.unitW + myRoot.gutterW) - 0 * myRoot.unitW / 75);
                    btnsArray[_loc_1].y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75 + 14 * _loc_1 * myRoot.unitW / 75);
                    _loc_1++;
                }
            }
            return;
        }

        function saveBtnClick(event:MouseEvent)
        {
            myRoot.bookmarkLightbox();
            return;
        }

        function updateBookmarksCount(event:BookmarksDeleteEvent) : void
        {
            if (event.pageCount != null)
            {
                navArray[0] = "My Pages " + event.pageCount;
                btnsArray[0].field.text = navArray[0];
            }
            if (event.imageCount != null)
            {
                navArray[1] = "My Images " + event.imageCount;
                btnsArray[1].field.text = navArray[1];
            }
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function buildLongTitle() : void
        {
            var _loc_1:String = null;
            _loc_1 = "<span class=\'myBold\'>Bookmark Set #" + myRoot.myBookmarksID + "</span><br>Created " + myRoot.bookmarksXML.@date;
            longText = new LongText(myRoot, _loc_1);
            longText.alpha = 1;
            addChild(longText);
            doResize();
            return;
        }

        function btnRollOver(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.overColor, alpha:1, overwrite:true});
            }
            else
            {
                TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.hotColor, alpha:1, overwrite:true});
            }
            return;
        }

        function btnRollOut(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.coldColor, alpha:1, overwrite:true});
            }
            return;
        }

        function switchShells(param1:int) : void
        {
            var i:int;
            var incomingId:* = param1;
            currNavId = incomingId;
            TweenMax.to(msgText, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                msgText.visible = false;
                return;
            }
            });
            i;
            while (i < btnsArray.length)
            {
                
                if (i == currNavId)
                {
                    TweenMax.to(btnsArray[i], fadeSpeed, {tint:16724736, alpha:1, overwrite:true});
                    trace("shellsArray[currNavId] " + shellsArray[currNavId]);
                    if (shellsArray[currNavId] != undefined)
                    {
                        if (currNavId == 0)
                        {
                        }
                        else if (currNavId == 1)
                        {
                        }
                        shellsArray[currNavId].visible = true;
                        shellsArray[currNavId].fadeInShell();
                    }
                    else
                    {
                        createNewShell(currNavId);
                    }
                }
                else
                {
                    TweenMax.to(btnsArray[i], fadeSpeed, {tint:7763574, alpha:1, overwrite:true});
                    if (shellsArray[i] != undefined)
                    {
                        shellsArray[i].fadeOutShell();
                    }
                }
                i = (i + 1);
            }
            if (currNavId == 0)
            {
                myRoot.currBookmark = "page";
            }
            else if (currNavId == 1)
            {
                myRoot.currBookmark = "image";
            }
            return;
        }

        function saveBtnRollOver(event:MouseEvent)
        {
            TweenMax.to(myLink.saveBG, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            return;
        }

        function loadBookmarks() : void
        {
            myBookmarksID = myRoot.myBookmarksID;
            bookmarksXMLLoader = new URLLoader();
            bookmarksXMLLoader.addEventListener(Event.COMPLETE, initBookmarksXML);
            bookmarksXMLLoader.load(new URLRequest(myRoot.path + "lightbox_support/load_lightbox_xml.php?lightbox_id=" + myBookmarksID));
            return;
        }

        function buildNav() : void
        {
            var _loc_1:int = 0;
            btnsArray = new Array();
            shellsArray = new Array();
            shellsArray.length = navArray.length;
            _loc_1 = 0;
            while (_loc_1 < navArray.length)
            {
                
                btnsArray[_loc_1] = new NavItem(myRoot, navArray[_loc_1], "", _loc_1);
                btnsArray[_loc_1].addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
                btnsArray[_loc_1].addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
                btnsArray[_loc_1].addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                btnsArray[_loc_1].alpha = 0;
                addChild(btnsArray[_loc_1]);
                TweenMax.to(btnsArray[_loc_1], fadeSpeed, {alpha:1, overwrite:true});
                _loc_1++;
            }
            doResize();
            if (myRoot.currBookmark == "page")
            {
                currNavId = 0;
            }
            else
            {
                currNavId = 1;
            }
            switchShells(currNavId);
            addChild(coverSprite);
            return;
        }

        function btnClick(event:MouseEvent) : void
        {
            switchShells(event.currentTarget.idNum);
            return;
        }

        function createNewShell(param1:int) : void
        {
            switch(param1)
            {
                case 0:
                {
                    if (bookmarksXML.bookmarks.item.length() > 0)
                    {
                        bookmarksPagesShell = new BookmarksPagesShell(myRoot, fadeSpeed, bookmarksXML.bookmarks.item);
                        shellsArray[param1] = bookmarksPagesShell;
                        shellsArray[param1].alpha = 0;
                        addChild(shellsArray[param1]);
                        bookmarksPagesShell.addEventListener(BookmarksDeleteEvent.BOOKMARKS_DELETE, updateBookmarksCount, false, 0, true);
                        doResize();
                        shellsArray[param1].fadeInShell();
                        addChild(coverSprite);
                    }
                    else
                    {
                        msgText.field.text = "You have not bookmarked any pages yet.";
                        msgText.visible = true;
                        TweenMax.to(msgText, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
                    }
                    break;
                }
                case 1:
                {
                    if (bookmarksXML.images.item.length() > 0)
                    {
                        bookmarksImgShell = new BookmarksImgShell(myRoot, fadeSpeed, bookmarksXML.images.item);
                        shellsArray[param1] = bookmarksImgShell;
                        shellsArray[param1].alpha = 0;
                        addChild(shellsArray[param1]);
                        bookmarksImgShell.addEventListener(BookmarksDeleteEvent.BOOKMARKS_DELETE, updateBookmarksCount, false, 0, true);
                        doResize();
                        shellsArray[param1].fadeInShell();
                        addChild(coverSprite);
                    }
                    else
                    {
                        msgText.field.text = "You have not bookmarked any images yet.";
                        msgText.visible = true;
                        TweenMax.to(msgText, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
                    }
                    break;
                }
                case 2:
                {
                    bookmarksNote = new BookmarksNote(myRoot, fadeSpeed, bookmarksXML.notes);
                    shellsArray[param1] = bookmarksNote;
                    shellsArray[param1].alpha = 0;
                    addChild(shellsArray[param1]);
                    doResize();
                    shellsArray[param1].fadeInShell();
                    addChild(coverSprite);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        function focusInListener(event:FocusEvent) : void
        {
            if (stage.displayState == StageDisplayState.FULL_SCREEN)
            {
                myRoot.closeFullscreen();
            }
            return;
        }

        function saveBtnRollOut(event:MouseEvent)
        {
            TweenMax.to(myLink.saveBG, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            return;
        }

    }
}
